import Vue from "vue";
import Router from "vue-router";
import Global from './Global'
Vue.use(Router);

const router = new Router({
  mode: "hash",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "login",
      component: () => import("./views/login/login.vue")
    },
    {
      path: "/home",
      name: "home",
      component: () => import("./views/Home.vue"),
      children: [
        {
          path: "/aboutPc",
          name: "首页",
          component: () => import("./views/about/aboutPc.vue")
        },
        {
          path: "/home",
          name: "艺人搜索",
          component: () => import("./views/artistsSearch/artistsSearch.vue")
        },
        {
          path: "/projectList",
          name: "所有项目",
          component: () => import("./views/projectList/projectList.vue")
        },
        {
          path: "/myProject",
          name: "我的项目",
          component: () => import("./views/myProject/myProject.vue")
        },
        {
          path: "/myJoinProject",
          name: "我参加的项目",
          component: () => import("./views/myProject/myJoinProject.vue")
        },
        {
          path: "/nameList",
          name: "名单",
          component: () => import("./views/myProject/nameList.vue")
        },
        {
          path: "/artistManagement",
          name: "艺人管理",
          component: () =>
            import("./views/artistManagement/artistManagement.vue")
        },
        {
          path: "/artistDetail",
          name: "艺人详情",
          component: () =>
            import("./views/artistManagement/artistDetail.vue")
        },
        {
          path: "/myCollection",
          name: "我的收藏",
          component: () => import("./views/myCollection/myCollection.vue")
        },
        {
          path: "/usrinfo",
          name: "用户信息",
          component: () => import("./views/info/usrinfo.vue"),
          children: [
            {
              path: "/account",
              name: "账号中心",
              component: () => import("./components/account.vue")
            },
            {
              path: "/changepassword",
              name: "个人信息",
              component: () => import("./components/changepassword.vue")
            }
          ]
        }
      ]
    },

    {
      path: "/actorHome",
      name: "actorHome",
      component: () => import("./views/Home.vue"),
      children: [{
        path: "/actorAboutPc",
        name: "首页",
        component: () =>
          import("./views/about/aboutPc.vue")
      },
        {
          path: "/newArtistDetail1",
          name: "艺人详情",
          component: () =>
            import("./views/share/artistDetail.vue")
        },
      ]
    },
    {
      path: "/newArtistDetail",
      name: "艺人详情",
      component: () =>
        import("./views/share/artistDetail.vue")
    },
    {
      path: "/artistAdd",
      name: "新增艺人",
      component: () =>
        import("./views/share/artistAdd.vue")
    },
    {
      path: "/shareNameList",
      name: "项目名单",
      component: () =>
        import("./views/share/nameList.vue")
    },
    {
      path: "/projectDetail",
      name: "项目详情",
      component: () =>
        import("./views/projectList/projectDetail.vue")
    },
    {
      path: "/about",
      name: "联系平台",
      component: () => import("./views/about/about.vue")
    },
  ]
});

router.beforeEach((to, from, next) => {
  const token = Global.token;
  if (!token && to.name !== "login") {
    //如果是详情不需要token
    if (to.name === "艺人详情"
      || to.name === "项目名单"
      || to.name === '联系平台'
      || to.name === '项目详情') {
      next()
    } else {
      // 未登录且要跳转的页面不是登录页
      next({
        name: "login" // 跳转到登录页
      })
    }
  } else {
    next()
  }
});

export default router
